using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class MainMenuUI : MonoBehaviour
{
    [Header("UI References")]
    [SerializeField] private RectTransform mainPanel;
    [SerializeField] private RectTransform titlePanel;
    [SerializeField] private CanvasGroup fadeGroup;
    
    [Header("Game Buttons")]
    [SerializeField] private GameButton numberPuzzleButton;
    [SerializeField] private GameButton memoryMatchButton;
    [SerializeField] private GameButton tetrisButton;
    [SerializeField] private GameButton connectClearButton;

    [Header("Animation Settings")]
    [SerializeField] private float fadeInDuration = 0.5f;
    [SerializeField] private float buttonAnimationDelay = 0.1f;
    
    private void Start()
    {
        InitializeUI();
        PlayEntranceAnimation();
        InitializeButtons();
    }

    private void InitializeUI()
    {
        // 初始化UI元素
        if (fadeGroup != null)
            fadeGroup.alpha = 0f;
            
        if (mainPanel != null)
            mainPanel.localScale = Vector3.zero;
            
        if (titlePanel != null)
            titlePanel.localScale = Vector3.zero;
    }

    private void PlayEntranceAnimation()
    {
        // 标题动画
        if (titlePanel != null)
        {
            titlePanel.DOScale(1f, fadeInDuration)
                .SetEase(Ease.OutBack);
        }

        // 主面板动画
        if (mainPanel != null)
        {
            mainPanel.DOScale(1f, fadeInDuration)
                .SetEase(Ease.OutBack)
                .SetDelay(fadeInDuration * 0.5f);
        }

        // 渐入动画
        if (fadeGroup != null)
        {
            fadeGroup.DOFade(1f, fadeInDuration);
        }
    }

    private void InitializeButtons()
    {
        // 设置按钮文本和图标
        if (numberPuzzleButton != null)
        {
            numberPuzzleButton.SetupButton("数字拼图");
            numberPuzzleButton.GetComponent<Button>().onClick.AddListener(() => LoadGame(GameManager.GameType.NumberPuzzle));
        }
        
        if (memoryMatchButton != null)
        {
            memoryMatchButton.SetupButton("记忆翻牌");
            memoryMatchButton.GetComponent<Button>().onClick.AddListener(() => LoadGame(GameManager.GameType.MemoryMatch));
        }
        
        if (tetrisButton != null)
        {
            tetrisButton.SetupButton("俄罗斯方块");
            tetrisButton.GetComponent<Button>().onClick.AddListener(() => LoadGame(GameManager.GameType.Tetris));
        }
        
        if (connectClearButton != null)
        {
            connectClearButton.SetupButton("连线消除");
            connectClearButton.GetComponent<Button>().onClick.AddListener(() => LoadGame(GameManager.GameType.ConnectClear));
        }
    }

    private void LoadGame(GameManager.GameType gameType)
    {
        // 播放退出动画
        PlayExitAnimation(() => {
            GameManager.Instance.SetCurrentGame(gameType);
            SceneLoader.Instance.LoadGame(gameType);
        });
    }

    private void PlayExitAnimation(System.Action onComplete)
    {
        if (fadeGroup != null)
        {
            fadeGroup.DOFade(0f, fadeInDuration * 0.5f)
                .OnComplete(() => onComplete?.Invoke());
        }
        else
        {
            onComplete?.Invoke();
        }
    }
}
